/**
 * 订单列表的js操作
 */
//统一调用
getOrderList();

/**
 * 获取所有订单信息
 */
function getOrderList() {
    //ajax
    $.ajax({
        url: '/api/order/getList',
        type: 'post',
        data: {},
        dataType: 'json',
        success: function (data) {
            var html = "";
            $.each(data, function (index, item) {
                //遍历当前订单下的详情，统计总金额
                var sum = 0;
                $.each(item.orderDetails, function (i, detail) {
                    sum += detail.price * detail.count;
                });

                html += '\t\t\t\t\t<div class="order-box ' + (item.status < 1 ? 'order-pay' : '') + '">\n' +
                    '\t\t\t\t\t\t<div class="order-header">\n' +
                    '\t\t\t\t\t\t\t<div class="status ' + (item.status < 1 ? 'status-pay' : '') + '">\n'
                    + ['等待付款', '等待发货', '运送中', '已收货', '退货中', '已退货'][item.status] +
                    '\t\t\t\t\t\t\t</div>\n' +
                    '\t\t\t\t\t\t</div>\n' +
                    '\t\t\t\t\t\t\n' +
                    '\t\t\t\t\t\t<table class="detail">\n' +
                    '\t\t\t\t\t\t\t<thead>\n' +
                    '\t\t\t\t\t\t\t\t<tr>\n' +
                    '\t\t\t\t\t\t\t\t\t<th class="info">' + item.time + '| ' + (item.address.contact != null ? item.address.contact : '') + ' | 订单号：' + item.id + ' | 在线支付（支付宝快捷支付）</th>\n' +
                    '\t\t\t\t\t\t\t\t\t<th class="money">应付金额： <span>'+sum+'</span> 元</th>\n' +
                    '\t\t\t\t\t\t\t\t</tr>\n' +
                    '\t\t\t\t\t\t\t</thead>\n' +
                    '\t\t\t\t\t\t\t<tbody>\n'
                $.each(item.orderDetails,function (i,detail){

                    html += '\t\t\t\t\t\t\t\t<tr>\n' +
                    '\t\t\t\t\t\t\t\t\t<td class="goods">\n' +
                    '\t\t\t\t\t\t\t\t\t\t<div class="goods-box">\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t<div class="img">\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t\t<a href=""><img src="/upload/'+detail.goods.pictures[0].name+'" ></a>\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t</div>\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t<div class="goods-info">\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t\t<p class="name"><a href="/goods?id='+detail.goods.id+'">'+detail.goods.name+' '+detail.goods.version+' '+detail.goods.color+'</a></p>\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t\t<p class="price">'+detail.price+' x '+detail.count+'</p>\n' +
                    '\t\t\t\t\t\t\t\t\t\t\t</div>\n' +
                    '\t\t\t\t\t\t\t\t\t\t</div>\n' +
                    '\t\t\t\t\t\t\t\t\t</td>\n' +
                    '\t\t\t\t\t\t\t\t\t<td class="actions">\n' +
                        (item.status==0?'<a class="active" href="/user/order/pay?orderId='+item.id+'&addressId='+item.addressId+'">立即付款</a>':'')+
                    '\t\t\t\t\t\t\t\t\t\t<a href="">订单详情</a>\n' +
                    '\t\t\t\t\t\t\t\t\t\t<a href="">联系客服</a>\n' +
                    '\t\t\t\t\t\t\t\t\t</td>\n' +
                    '\t\t\t\t\t\t\t\t</tr>'
                });
                    html += '\t\t\t\t\t\t\t</tbody>\n' +
                    '\t\t\t\t\t\t</table>\n' +
                    '\t\t\t\t\t</div>';
            });

            $(".order-list").html(html);

        },
        error: function () {

        }
    });
}